package com.hnevc.yangfan;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class ZhuceActivity extends AppCompatActivity {

    private EditText name;
    private EditText phone;
    private EditText password;
    private Button zhuce;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reqsit);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        password = findViewById(R.id.password);
        zhuce = findViewById(R.id.zhuce);
    }
    private void initData() {
        String url ="http://10.1.62.39/api/user/register";
        String nameStr =name.getText().toString().trim();
        String phoneStr=phone.getText().toString().trim();
        String passwordStr=password.getText().toString().trim();
        final JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("telephone",phoneStr);
            jsonObject.put("name",nameStr);
            jsonObject.put("password",passwordStr);
        }catch (JSONException e){
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                Toast.makeText(ZhuceActivity.this,jsonObject.optString("msg")+"",Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ZhuceActivity.this,"网络失败",Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue queue = Volley.newRequestQueue(ZhuceActivity.this);
        queue.add(jsonObjectRequest);
    }

    private void initEvent() {
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }
}
